package com.fone.player.storage.download;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.fone.player.entity.OfflineCache;
import com.fone.player.storage.OfflineCacheFileDataBaseAdapter;
import com.fone.player.storage.OfflineCacheFolderDataBaseAdapter;
import com.fone.player.storage.OfflineCacheModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.L;

/**
 * 
 * 离线缓存下载管理
 * 
 * @author wangyang
 * @since 2014年6月13日
 */
public class DownloadOfflineCacheManager {
	private static final String TAG = DownloadOfflineCacheManager.class
			.getSimpleName();
	/**
	 * 最大下载数量
	 */
	public static final int MAX_DOWNLOAD_NUM = 2;
	private DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable> mDownloadRunnableActiveQueue = new DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable>();
	private DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable> mDownloadRunnableUnactiveUserQueue = new DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable>();
	private DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable> mDownloadRunnableUnactiveAutoQueue = new DownloadOfflineCacheQueue<String, DownloadOfflineCacheRunnable>();
	private static DownloadMessageProtocol sDownloadMessageProtocol = null;
	private static final DownloadOfflineCacheManager mInstance = new DownloadOfflineCacheManager();

	public static DownloadOfflineCacheManager getInstance() {
		return mInstance;
	}

	private DownloadOfflineCacheManager() {
	}

	public static void init(DownloadMessageProtocol downloadMessageProtocol) {
		L.v(TAG, "init", "start downloadMessageProtocol="
				+ downloadMessageProtocol);
		sDownloadMessageProtocol = downloadMessageProtocol;
	}

	/**
	 * 
	 * 获取当前正在下载的集合
	 * 
	 * @return ArrayList<DownloadRunnable> 当前正在下载的集合
	 */
	public ArrayList<DownloadOfflineCacheRunnable> getDownloadRunnableList() {
		ArrayList<DownloadOfflineCacheRunnable> downloadRunnableList = new ArrayList<DownloadOfflineCacheRunnable>();
		downloadRunnableList
				.addAll(mDownloadRunnableUnactiveUserQueue.values());
		return downloadRunnableList;
	}

	/**
	 * 初始化下载队列
	 */
	public void initQueue() {

		List<OfflineCache> offlineCacheNotFinishList = OfflineCacheFileDataBaseAdapter
				.getInstance().getOfflineCacheFileNotFinishList();
		if (offlineCacheNotFinishList != null
				&& offlineCacheNotFinishList.size() > 0) {

			// 加入队列
			addList(offlineCacheNotFinishList);

		} else {

			L.v(TAG, "initQueue", "offlineCacheNotFinishList=null");

		}

	}

	/**
	 * 
	 * 下载任务
	 * 
	 * @param download
	 *            下载对象
	 * @return int 操作结果<br>
	 *         1 添加成功<br>
	 *         -1 任务已存在 <br>
	 *         -2添加异常
	 */
	public void addList(List<OfflineCache> offlineCacheList) {
		L.v(TAG, "addList", "start size=" + offlineCacheList.size()
				+ " sDownloadMessageProtocol=" + sDownloadMessageProtocol);

		for (OfflineCache offlineCache : offlineCacheList) {
			add(offlineCache);
		}

	}

	/**
	 * 
	 * 添加单个视频
	 * 
	 * @param offlineCache
	 *            视频信息
	 * @return void
	 */
	public void add(OfflineCache offlineCache) {

		L.v(TAG, "add", "start name=" + offlineCache.getCacheName()
				+ offlineCache.getCacheEpisodeNum());
		// key=cid+_+dfnt
		String key = offlineCache.getCacheCID() + "_"
				+ offlineCache.getCacheDefinitionType();

		// 是否重复
		boolean isRepeat = mDownloadRunnableActiveQueue.containsKey(key)
				|| mDownloadRunnableUnactiveUserQueue.containsKey(key)
				|| mDownloadRunnableUnactiveAutoQueue.containsKey(key);
		// 判断任务是否重复
		if (isRepeat) {

			DownloadOfflineCacheRunnable downloadRunnable = mDownloadRunnableActiveQueue
					.get(key);

			if (downloadRunnable == null) {

				downloadRunnable = mDownloadRunnableUnactiveUserQueue.get(key);

				if (downloadRunnable == null) {

					downloadRunnable = mDownloadRunnableUnactiveAutoQueue
							.get(key);

				}
			}

			if (downloadRunnable != null) {

				switch (downloadRunnable.getRunnableState()) {
				case DownloadOfflineCacheRunnable.STATE_RUNNING:
					// 重复
					L.w(TAG, "add", "name=" + offlineCache.getCacheName()
							+ offlineCache.getCacheEpisodeNum()
							+ " already repeat");
					break;
				case DownloadOfflineCacheRunnable.STATE_STOP:
					L.w(TAG, "add", "name=" + offlineCache.getCacheName()
							+ offlineCache.getCacheEpisodeNum()
							+ " delete already stop task");
					// 删除已停止的任务
					mDownloadRunnableActiveQueue.remove(key);
					mDownloadRunnableUnactiveUserQueue.remove(key);
					mDownloadRunnableUnactiveAutoQueue.remove(key);
					break;
				default:
					break;
				}

			}

		} else {

			// 创建下载任务,开始下载
			DownloadOfflineCacheRunnable downloadRunnable = new DownloadOfflineCacheRunnable();
			downloadRunnable.setOfflineCache(offlineCache);
			downloadRunnable.setHandler(mHandler);

			// 判断是否超过规定的下载数量
			if (mDownloadRunnableActiveQueue.size() < MAX_DOWNLOAD_NUM) {

				// 判断缓存来自哪个界面
				switch (offlineCache.getCacheFromPage()) {
				case OfflineCache.CACHE_FROM_PAGE_DETAIL:
					// 详情界面,停止下载
					downloadRunnable
							.setOperateType(DownloadOfflineCacheRunnable.OPERATE_STOP);
					break;
				default:
					// 其他界面,开始下载
					downloadRunnable
							.setOperateType(DownloadOfflineCacheRunnable.OPERATE_RUNNING);
					ThreadPoolManager.getInstance().addFileTask(
							downloadRunnable);
					break;
				}

				// 放到活跃队列里面
				L.v(TAG,
						"add",
						"add activeQueue cacheFromPage="
								+ offlineCache.getCacheFromPage());
				mDownloadRunnableActiveQueue.put(key, downloadRunnable);

				// 保存json
				SaveCacheDataUtil.getInstance().saveCacheJSONFile(offlineCache);

			} else {

				L.v(TAG, "add", "add activeAutoQueue cacheFromPage="
						+ offlineCache.getCacheFromPage());
				// 放到非活跃队列里面
				mDownloadRunnableUnactiveAutoQueue.put(key, downloadRunnable);
			}

		}

	}

	/**
	 * 
	 * 自动开始任务
	 * 
	 * @return void
	 */
	public void startAuto() {

		L.v(TAG, "startAuto", "start");

		// 判断活跃队列是否超过规定的下载数量
		if (mDownloadRunnableActiveQueue.size() < MAX_DOWNLOAD_NUM) {

			L.v(TAG, "startAuto",
					"remove start mDownloadRunnableUnactiveAutoQueue.size="
							+ mDownloadRunnableUnactiveAutoQueue.size());
			// 根据键获取非活跃自动下载队列的任务
			DownloadOfflineCacheRunnable downloadRunnableUnactiveAutoFirst = mDownloadRunnableUnactiveAutoQueue
					.removeFirst();

			L.v(TAG, "startAuto",
					"remove end mDownloadRunnableUnactiveAutoQueue.size="
							+ mDownloadRunnableUnactiveAutoQueue.size());

			// 非空判断
			if (downloadRunnableUnactiveAutoFirst != null) {

				downloadRunnableUnactiveAutoFirst
						.setOperateType(DownloadOfflineCacheRunnable.OPERATE_RUNNING);

				OfflineCache offlineCache = downloadRunnableUnactiveAutoFirst
						.getOfflineCache();
				switch (offlineCache.getCacheDownloadState()) {
				case OfflineCache.CACHE_STATE_NOT_DOWNLOAD:
				case OfflineCache.CACHE_STATE_DOWNLOADING:
				case OfflineCache.CACHE_STATE_WAITING:
					L.v(TAG, "startAuto",
							"auto start name=" + offlineCache.getCacheName()
									+ offlineCache.getCacheEpisodeNum());

					// 放到活跃队列里面
					ThreadPoolManager.getInstance().addFileTask(
							downloadRunnableUnactiveAutoFirst);

					L.v(TAG,
							"startAuto",
							"add activeQueue name="
									+ offlineCache.getCacheName()
									+ offlineCache.getCacheEpisodeNum());

					// key=cid+_+dfnt
					String key = offlineCache.getCacheCID() + "_"
							+ offlineCache.getCacheDefinitionType();

					mDownloadRunnableActiveQueue.put(key,
							downloadRunnableUnactiveAutoFirst);
					break;
				default:
					L.v(TAG,
							"startAuto",
							"jump auto start name="
									+ offlineCache.getCacheName()
									+ offlineCache.getCacheEpisodeNum());
					break;
				}

			} else {

				// 任务不存在或已经下载完,此处忽略
				L.v(TAG, "startAuto", "downloadRunnableUnactiveAutoFirst=null");
				StorageModule.getInstance().writeStackTrace();
			}

		} else {

			// 自动开始任务时,不进行强制开启任务,此处忽略
			L.v(TAG, "startAuto", "limit MAX_DOWNLOAD_NUM");
		}
	}

	/**
	 * 
	 * 开始任务
	 * 
	 * @param offlineCache
	 *            下载对象
	 * @return void
	 */
	public void start(OfflineCache offlineCache) {
		L.v(TAG, "start", "start name=" + offlineCache.getCacheName()
				+ offlineCache.getCacheEpisodeNum());

		L.v(TAG, "start",
				"remove start mDownloadRunnableUnactiveUserQueue.size="
						+ mDownloadRunnableUnactiveUserQueue.size());
		// key=cid+_+dfnt
		String key = offlineCache.getCacheCID() + "_"
				+ offlineCache.getCacheDefinitionType();

		// 根据键获取非活跃的任务
		DownloadOfflineCacheRunnable downloadRunnableUnactiveFirst = mDownloadRunnableUnactiveUserQueue
				.remove(key);

		L.v(TAG, "start", "remove end mDownloadRunnableUnactiveUserQueue.size="
				+ mDownloadRunnableUnactiveUserQueue.size());

		if (downloadRunnableUnactiveFirst == null) {
			// 下载出错,手动点击重试
			L.w(TAG, "start", "downloadRunnableUnactiveUserFirst=null");
			// 根据键获取活跃的任务
			downloadRunnableUnactiveFirst = mDownloadRunnableUnactiveAutoQueue
					.remove(key);

		}

		// 非空判断
		if (downloadRunnableUnactiveFirst != null) {

			// 判断活跃队列是否超过规定的下载数量
			if (mDownloadRunnableActiveQueue.size() < MAX_DOWNLOAD_NUM) {

				downloadRunnableUnactiveFirst
						.setOperateType(DownloadOfflineCacheRunnable.OPERATE_RUNNING);

				// 放到活跃队列里面
				ThreadPoolManager.getInstance().addFileTask(
						downloadRunnableUnactiveFirst);
				mDownloadRunnableActiveQueue.put(key,
						downloadRunnableUnactiveFirst);
			} else {

				// 判断活跃队列是否有任务
				if (mDownloadRunnableActiveQueue.size() > 0) {

					// 取活跃队列队首
					DownloadOfflineCacheRunnable downloadRunnableActiveFirst = mDownloadRunnableActiveQueue
							.removeFirst();

					// 停止任务
					downloadRunnableActiveFirst
							.setOperateType(DownloadOfflineCacheRunnable.OPERATE_STOP);

					// 将活跃任务添加到非活跃队列的末尾
					mDownloadRunnableUnactiveAutoQueue.put(key,
							downloadRunnableActiveFirst);

					// 将非活跃任务添加到活跃队列的末尾
					mDownloadRunnableActiveQueue.put(key,
							downloadRunnableUnactiveFirst);

					// 标记开始
					downloadRunnableUnactiveFirst
							.setOperateType(DownloadOfflineCacheRunnable.OPERATE_RUNNING);

					// 下载
					ThreadPoolManager.getInstance().addFileTask(
							downloadRunnableUnactiveFirst);
				}
			}

		} else {

			// 任务不存在,重新创建任务
			L.e(TAG, "start", "downloadRunnableActiveAutoFirst=null");

			add(offlineCache);
		}

	}

	/**
	 * 
	 * 开始所有任务
	 * 
	 * @param isUpdateDownloadState
	 *            是否更新下载状态
	 * @return void
	 */
	public void startAll() {
		L.v(TAG, "startAll", "start mDownloadRunnableActiveQueue.size="
				+ mDownloadRunnableActiveQueue.size());
		// 把活跃队列里面的所有任务都开始下载
		Iterator<Entry<String, DownloadOfflineCacheRunnable>> iterator = mDownloadRunnableActiveQueue
				.entrySet().iterator();
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		while (iterator.hasNext()) {
			Entry<String, DownloadOfflineCacheRunnable> entry = (Entry<String, DownloadOfflineCacheRunnable>) iterator
					.next();
			DownloadOfflineCacheRunnable downloadOfflineCacheActiveRunnable = entry
					.getValue();
			switch (downloadOfflineCacheActiveRunnable.getRunnableState()) {
			case DownloadOfflineCacheRunnable.STATE_RUNNING:
				// 已经启动的任务做忽略处理
				L.w(TAG, "startAll",
						"downloadOfflineCacheActiveRunnable already STATE_RUNNING");
				break;
			default:
				downloadOfflineCacheActiveRunnable
						.setOperateType(DownloadOfflineCacheRunnable.OPERATE_RUNNING);
				ThreadPoolManager.getInstance().addFileTask(
						downloadOfflineCacheActiveRunnable);
				// 更新数据库
				OfflineCacheFileDataBaseAdapter.getInstance()
						.updateOfflineCacheFileList(offlineCacheList);
				L.v(TAG, "startAll", "mDownloadRunnableActiveQueue.size="
						+ mDownloadRunnableActiveQueue.size());
				break;
			}
		}

	}

	/**
	 * 
	 * 暂停任务
	 * 
	 * @param offlineCache
	 *            下载对象
	 * @return void
	 */
	public void pause(OfflineCache offlineCache) {

		L.v(TAG, "pause", "start name=" + offlineCache.getCacheName()
				+ offlineCache.getCacheEpisodeNum());

		L.v(TAG, "pause", "remove start mDownloadRunnableActiveQueue.size="
				+ mDownloadRunnableActiveQueue.size());

		// key=cid+_+dfnt
		String key = offlineCache.getCacheCID() + "_"
				+ offlineCache.getCacheDefinitionType();

		// 根据键获取活跃的任务
		DownloadOfflineCacheRunnable downloadRunnableActiveFirst = mDownloadRunnableActiveQueue
				.remove(key);

		L.v(TAG, "pause", "remove end mDownloadRunnableActiveQueue.size="
				+ mDownloadRunnableActiveQueue.size());

		// 非空判断
		if (downloadRunnableActiveFirst != null) {

			L.v(TAG, "pause", "set operateType=OPERATE_STOP");

			// 停止当前活跃任务
			downloadRunnableActiveFirst
					.setOperateType(DownloadOfflineCacheRunnable.OPERATE_STOP);

			L.v(TAG, "pause",
					"add unactiveUserQueue name=" + offlineCache.getCacheName()
							+ offlineCache.getCacheEpisodeNum());
			// 把活跃任务添加到非活跃队列队尾
			mDownloadRunnableUnactiveUserQueue.put(key,
					downloadRunnableActiveFirst);

			// 自动开始下一个任务
			startAuto();

		} else {

			// 任务不存在或已经下载完或已经全部暂停
			L.e(TAG, "pause", "downloadRunnableActiveFirst=null");
		}
	}

	/**
	 * 
	 * 暂停所有任务
	 * 
	 * @param isUpdateDownloadState
	 *            是否更新下载状态
	 * @return void
	 */
	public void pauseAll(boolean isUpdateDownloadState) {
		L.v(TAG, "pauseAll", "start isUpdateDownloadState="
				+ isUpdateDownloadState + " mDownloadRunnableActiveQueue.size="
				+ mDownloadRunnableActiveQueue.size());

		// 把活跃队列里面的所有任务都停止下载,不删除
		Iterator<Entry<String, DownloadOfflineCacheRunnable>> iterator = mDownloadRunnableActiveQueue
				.entrySet().iterator();
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		while (iterator.hasNext()) {
			Entry<String, DownloadOfflineCacheRunnable> entry = (Entry<String, DownloadOfflineCacheRunnable>) iterator
					.next();
			DownloadOfflineCacheRunnable downloadOfflineCacheActiveRunnable = entry
					.getValue();
			switch (downloadOfflineCacheActiveRunnable.getRunnableState()) {
			case DownloadOfflineCacheRunnable.STATE_RUNNING:

				downloadOfflineCacheActiveRunnable
						.setOperateType(isUpdateDownloadState ? DownloadOfflineCacheRunnable.OPERATE_STOP_BATCH_UPDATE_STATE
								: DownloadOfflineCacheRunnable.OPERATE_STOP_BATCH_UNUPDATE_STATE);

				OfflineCache offlineCache = downloadOfflineCacheActiveRunnable
						.getOfflineCache();

				offlineCacheList.add(offlineCache);

				// 更新数据库
				OfflineCacheFileDataBaseAdapter.getInstance()
						.updateOfflineCacheFileList(offlineCacheList);

				L.v(TAG, "pauseAll", "mDownloadRunnableActiveQueue.size="
						+ mDownloadRunnableActiveQueue.size());
				break;
			default:
				// 已经停止的任务做忽略处理
				L.w(TAG, "pauseAll",
						"downloadOfflineCacheActiveRunnable already STATE_STOP");
				break;
			}

		}

	}

	/**
	 * 
	 * 处理下载成功的任务
	 * 
	 * @param offlineCache
	 *            视频信息
	 * @return void
	 */
	public void handleFinish(OfflineCache offlineCache) {

		// key=cid+_+dfnt
		String key = offlineCache.getCacheCID() + "_"
				+ offlineCache.getCacheDefinitionType();

		DownloadOfflineCacheRunnable offlineCacheActiveRunnable = mDownloadRunnableActiveQueue
				.remove(key);

		if (offlineCacheActiveRunnable == null) {
			L.v(TAG,
					"handleFinish",
					"offlineCacheActiveRunnable=null name="
							+ offlineCache.getCacheName()
							+ offlineCache.getCacheEpisodeNum());
		} else {

			L.v(TAG, "handleFinish", "name=" + offlineCache.getCacheName()
					+ offlineCache.getCacheEpisodeNum());

		}

		// 自动下载下一个任务
		startAuto();
	}

	/**
	 * 
	 * 处理下载错误任务
	 * 
	 * @param offlineCache
	 *            视频信息
	 * @return void
	 */
	public void handleError(OfflineCache offlineCache) {

		// key=cid+_+dfnt
		String key = offlineCache.getCacheCID() + "_"
				+ offlineCache.getCacheDefinitionType();

		DownloadOfflineCacheRunnable offlineCacheActiveRunnable = mDownloadRunnableActiveQueue
				.remove(key);

		if (offlineCacheActiveRunnable != null) {

			// 把出错的添加到非活跃队列
			mDownloadRunnableUnactiveUserQueue.put(key,
					offlineCacheActiveRunnable);
		} else {
			L.v(TAG,
					"handleError",
					"offlineCacheActiveRunnable=null name="
							+ offlineCache.getCacheName()
							+ offlineCache.getCacheEpisodeNum());
		}

		// 自动下载下一个任务
		startAuto();
	}

	/**
	 * 
	 * 删除任务
	 * 
	 * @param download
	 *            下载对象
	 * @return void
	 */
	public void delete(List<OfflineCache> offlineCacheList) {
		L.v(TAG, "delete",
				"start offlineCacheList.size=" + offlineCacheList.size());
		for (OfflineCache offlineCache : offlineCacheList) {

			// key=cid+_+dfnt
			String key = offlineCache.getCacheCID() + "_"
					+ offlineCache.getCacheDefinitionType();

			// 停止下载,把任务从活跃队列和非活跃队列中删除
			DownloadOfflineCacheRunnable downloadOfflineCacheActiveRunnable = mDownloadRunnableActiveQueue
					.remove(key);
			if (downloadOfflineCacheActiveRunnable != null) {
				L.v(TAG, "delete",
						"activeQueue name=" + offlineCache.getCacheName()
								+ offlineCache.getCacheEpisodeNum());
				switch (downloadOfflineCacheActiveRunnable.getRunnableState()) {
				case DownloadOfflineCacheRunnable.STATE_RUNNING:
					// 运行中删除
					downloadOfflineCacheActiveRunnable
							.setOperateType(DownloadOfflineCacheRunnable.OPERATE_DELETE);
					break;
				case DownloadOfflineCacheRunnable.STATE_STOP:
					// 删除已停止的任务
					OfflineCacheModule.getInstance().deleteOfflineCacheFile(
							offlineCache);
					break;
				default:
					break;
				}

			} else {
				DownloadOfflineCacheRunnable downloadOfflineCacheUnactiveUserRunnable = mDownloadRunnableUnactiveUserQueue
						.remove(key);
				if (downloadOfflineCacheUnactiveUserRunnable != null) {
					L.v(TAG,
							"delete",
							"unActiveUserQueue name="
									+ offlineCache.getCacheName()
									+ offlineCache.getCacheEpisodeNum());
					switch (downloadOfflineCacheUnactiveUserRunnable
							.getRunnableState()) {
					case DownloadOfflineCacheRunnable.STATE_RUNNING:
						// 运行中删除
						downloadOfflineCacheUnactiveUserRunnable
								.setOperateType(DownloadOfflineCacheRunnable.OPERATE_DELETE);
						break;
					case DownloadOfflineCacheRunnable.STATE_STOP:
						// 删除已停止的任务
						OfflineCacheModule.getInstance()
								.deleteOfflineCacheFile(offlineCache);
						break;
					default:
						break;
					}
				} else {
					DownloadOfflineCacheRunnable downloadOfflineCacheUnactiveAutoRunnable = mDownloadRunnableUnactiveAutoQueue
							.remove(key);
					if (downloadOfflineCacheUnactiveAutoRunnable != null) {

						L.v(TAG,
								"delete",
								"unActiveAutoQueue name="
										+ offlineCache.getCacheName()
										+ offlineCache.getCacheEpisodeNum());

						switch (downloadOfflineCacheUnactiveAutoRunnable
								.getRunnableState()) {
						case DownloadOfflineCacheRunnable.STATE_RUNNING:
							// 运行中删除
							downloadOfflineCacheUnactiveAutoRunnable
									.setOperateType(DownloadOfflineCacheRunnable.OPERATE_DELETE);
							break;
						case DownloadOfflineCacheRunnable.STATE_STOP:
							// 删除已停止的任务
							OfflineCacheModule.getInstance()
									.deleteOfflineCacheFile(offlineCache);
							break;
						default:
							break;
						}

					} else {

						L.v(TAG, "delete",
								"finish name=" + offlineCache.getCacheName()
										+ offlineCache.getCacheEpisodeNum());

						OfflineCacheModule.getInstance()
								.deleteOfflineCacheFile(offlineCache);
					}
				}
			}

		}

		// 删除相关文件和数据
		deleteData(offlineCacheList);

		// 删除成功后,尝试开启等待中的任务开始下载
		startAuto();
	}

	/**
	 * 
	 * 根据缓存对象删除缓存文件(此方法内部使用,外部不需要调用)
	 * 
	 * @return void
	 */
	private void deleteData(List<OfflineCache> offlineCacheList) {
		L.v(TAG, "deleteFile", "start");
		long startTime = System.currentTimeMillis();
		L.v(TAG, "deleteFile", "delete database offlineCacheFile");
		// 删除文件层级信息
		OfflineCacheFileDataBaseAdapter.getInstance()
				.deleteOfflineCacheFileByCID(offlineCacheList);

		L.v(TAG, "deleteFile", "delete database offlineCacheFolder");
		// 删除文件夹层级数据
		OfflineCacheFolderDataBaseAdapter.getInstance()
				.deleteOfflineCacheFolder();

		long endTime = System.currentTimeMillis();
		L.v(TAG, "deleteFile", "end intervalTime=" + (endTime - startTime));
	}

	private static Handler mHandler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			if (bundle != null) {
				OfflineCache offlineCache = bundle
						.getParcelable(OfflineCache.OFFLINE_CACHE);
				if (offlineCache != null) {
					switch (offlineCache.getCacheDownloadState()) {
					case OfflineCache.CACHE_STATE_ERROR:
						L.v(TAG, "handleMessage",
								"name=" + offlineCache.getCacheName()
										+ offlineCache.getCacheEpisodeNum()
										+ " downloadState=CACHE_STATE_ERROR");
						// 处理错误
						DownloadOfflineCacheManager.getInstance().handleError(
								offlineCache);

						break;
					case OfflineCache.CACHE_STATE_FINISH:
						L.v(TAG, "handleMessage",
								"name=" + offlineCache.getCacheName()
										+ offlineCache.getCacheEpisodeNum()
										+ " downloadState=CACHE_STATE_FINISH");

						if (sDownloadMessageProtocol != null) {

							sDownloadMessageProtocol
									.sendFinishMessage(offlineCache);

						} else {

							L.e(TAG, "handleMessage",
									"sDownloadMessageProtocol=null");

						}

						// 完成一个任务,然后自动开启一个任务
						DownloadOfflineCacheManager.getInstance().handleFinish(
								offlineCache);

						break;
					default:
						break;
					}

					if (sDownloadMessageProtocol != null) {

						sDownloadMessageProtocol
								.sendDownloadingMessage(offlineCache);

					} else {

						L.e(TAG, "handleMessage",
								"sendDownloadingMessage sDownloadMessageProtocol=null");

					}

				}

			}

		}
	};

}
